cc.Class({
    extends: cc.Component,

    properties: {
        nums:{
            default:[],
            type:[cc.Label]
        },
        _inputIndex:0
        // foo: {
        //    default: null,
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
    },

    // use this for initialization
    onLoad: function () {
        
    },
    
    onEnable:function(){
        this.onResetClicked()
    },
    
    onInputFinished:function(roomId){
        cc.vv.userMgr.enterRoom(roomId,function(ret){
            if(ret.errcode == 0){
                this.node.active = false
            }else{
                var content = "房间["+ roomId +"]不存在，请重新输入!";
                if(ret.errcode == 4){
                    content = "房间["+ roomId + "]已满!"
                };
                cc.vv.alert.show("提示",content);
                this.onResetClicked()
            }
        }.bind(this)) 
    },
    
    onInput:function(num){
        if(this._inputIndex >= this.nums.length){
            return
        };
        this.nums[this._inputIndex].string = num;
        this._inputIndex += 1;
        
        if(this._inputIndex == this.nums.length&&!this.node.isYaoqing){
            var roomId = this.parseRoomID();
            // console.log("ok:" + roomId);
            this.onInputFinished(roomId)
        }
    },
    onNumClick:function(event){
        this.onInput(event.target.name)
    },
    
    onResetClicked:function(){
        for(var i = 0; i < this.nums.length; ++i){
            this.nums[i].string = ""
        };
        this._inputIndex = 0
    },
    onQuerenClicked:function(){
        console.log("onQuerenClicked");
        var agent = this.parseRoomID();
        this.node.active = false;
        var self=this;
        var onGet = function(ret){
            if(ret.errcode !== 0){
                var ms="";
                if (ret.errmsg!="ok") {
                    ms=ret.errmsg
                };
                cc.vv.alert.show("提示","绑定失败,请重新绑定\n失败原因："+ms);
                console.log(ret.errmsg)
            }else{
                if(ret.gems != null){
                    cc.vv.userMgr.hasagent=ret.hasagent;
                    self.node.parent.getComponent("Hall").lblGems.string=ret.gems;
                    self.node.parent.getChildByName("right_bottom").getChildByName("btn_yaoqing").active=false;
                    cc.vv.alert.show("提示","成功绑定邀请，房卡已赠送")
                }
            }
        };
        var data = {
            account:cc.vv.userMgr.account,
            sign:cc.vv.userMgr.sign,
            userid:cc.vv.userMgr.userId,
            agent:agent
        };
        cc.vv.http.sendRequest("/bind_agent",data,onGet)
    },
    onDelClicked:function(){
        if(this._inputIndex > 0){
            this._inputIndex -= 1;
            this.nums[this._inputIndex].string = ""
        }
    },
    onCloseClicked:function(){
        this.node.active = false
    },
    
    parseRoomID:function(){
        var str = "";
        for(var i = 0; i < this.nums.length; ++i){
            str += this.nums[i].string
        };
        return str
    }

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
